package com.ruoyi.web.controller.biz;

import java.util.List;

import com.ruoyi.biz.domain.MaintainWarnUser;
import com.ruoyi.biz.domain.MaintainWarnUserInput;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.biz.service.IMaintainWarnUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2021-01-05
 */
@RestController
@RequestMapping("/system/maintain/warn/user")
public class MaintainWarnUserController extends BaseController
{
    @Autowired
    private IMaintainWarnUserService maintainWarnUserService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/list")
    @ApiOperation("查询保养管理告警联系人列表")
    public TableDataInfo list(MaintainWarnUser maintainWarnUser)
    {
        startPage();
        List<MaintainWarnUser> list = maintainWarnUserService.selectMaintainWarnUserList(maintainWarnUser);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation("导出保养管理告警联系人信息")
    public AjaxResult export(MaintainWarnUser maintainWarnUser)
    {
        List<MaintainWarnUser> list = maintainWarnUserService.selectMaintainWarnUserList(maintainWarnUser);
        ExcelUtil<MaintainWarnUser> util = new ExcelUtil<MaintainWarnUser>(MaintainWarnUser.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping(value = "/{userId}")
    @ApiOperation("获取保养管理告警联系人信息")
    public AjaxResult getInfo(@PathVariable("userId") Integer userId)
    {
        return AjaxResult.success(maintainWarnUserService.selectMaintainWarnUserById(userId));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('operate:contact:manage:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增保养管理告警联系人信息")
    public AjaxResult add(@RequestBody MaintainWarnUserInput maintainWarnUserInput)
    {
        int result = 0;
        try {
            // 判断
            if (maintainWarnUserInput != null) {
                String[] userIds = maintainWarnUserInput.getSelectedContactIds();
                String[] maintainIds = maintainWarnUserInput.getSelectMaintainIds();
                // 表示要插入多少条记录
                for (int i = 0; i < userIds.length; i++) {
                    for (int j = 0; j < maintainIds.length; j++) {
                        MaintainWarnUser maintainWarnUser = new MaintainWarnUser();
                        maintainWarnUser.setUserId(Integer.parseInt(userIds[i]));
                        maintainWarnUser.setMaintainId(Integer.parseInt(maintainIds[j]));
                        result += maintainWarnUserService.insertMaintainWarnUser(maintainWarnUser);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("该联系人已与该设备关联，请勿重复关联");
        }
        return toAjax(result);
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改保养管理告警联系人信息")
    public AjaxResult edit(@RequestBody MaintainWarnUser maintainWarnUser)
    {
        return toAjax(maintainWarnUserService.updateMaintainWarnUser(maintainWarnUser));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:user:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    @ApiOperation("批量删除保养管理告警联系人信息")
    public AjaxResult remove(@PathVariable Integer[] userIds)
    {
        return toAjax(maintainWarnUserService.deleteMaintainWarnUserByIds(userIds));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('operate:contact:manage:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/{userId}/{maintainId}")
    @ApiOperation("删除保养管理告警联系人信息")
    public AjaxResult delete(@PathVariable("userId") Integer userId,@PathVariable("maintainId") Integer maintainId) {
        return toAjax(maintainWarnUserService.deleteMaintainWarnUserById(userId,maintainId));
    }
}
